/*
 * @Author: hx 2542123711@QQ.com
 * @Date: 2024-12-15 17:03:37
 * @LastEditors: hx 2542123711@QQ.com
 * @LastEditTime: 2024-12-15 20:14:50
 * @FilePath: /class_thread/thread-ringqueue/main.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include<iostream>
#include<unistd.h>
#include"RingQueue.hpp"
#include<vector>
#include<string>

using namespace std;

struct thread
{
    thread(int number)
    {
        rq = new RingQueue<int>();
        _name = "thread-" + to_string(number);
    }
    
    RingQueue<int>* rq;
    string _name;
};

void* Productor(void* args)
{
    //RingQueue<int>* rq = static_cast<RingQueue<int>*>(args);
    thread* t = static_cast<thread*>(args);
    RingQueue<int>* rq = t->rq;

    int data = 0;
    while(true)
    {
        rq->push(data);
        cout << "Productor: " << t->_name <<", data:  " << data++ << endl;
        sleep(1);
    }

    return nullptr;
}

void* Consumer(void* args)
{
    //RingQueue<int>* rq = static_cast<RingQueue<int>*>(args);
    thread* t = static_cast<thread*>(args);
    RingQueue<int>* rq = t->rq;

    while(true)
    {
        int data = 0;

        rq->pop(&data);
        //cout << data << endl;

        //cout << "Consumer: " << t->_name << endl;
        cout << "Consumer: " << t->_name << ", data: "<< data << endl;
        //sleep(1);
    }
}

int main()
{
    pthread_t c[2], p[3];

    // /RingQueue<int>* rq = new RingQueue<int>();

    for(int i=0; i<2; i++)
    {
        // thread* t = new thread();
        // t->rq = rq;
        // t->_name = "Productor-" + std::to_string(i);

        thread* t = new thread(i);
        pthread_create(c+i, nullptr, Productor, t);
    }

    for(int i=0; i<3; i++)
    {
        // thread* t = new thread();
        // t->rq = rq;
        // t->_name = "Consumer-" + std::to_string(i);

        thread* t = new thread(i);
        pthread_create(p+i, nullptr, Consumer, t);
    }

    sleep(1);
    
    for(int j = 0; j < 2; j++)
    {
        pthread_join(c[j], nullptr);
        //delete
    }

    for(int j = 0; j < 3; j++)
    {
        pthread_join(p[j], nullptr);
    }

    return 0;
}

